#!/bin/bash
#(c) copyright 2008 Barry Kauler
#2008 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v411 BK: wrapper for gparted, prevent indefinite scan at startup on some systems.

export TEXTDOMAIN=gparted_shell
export OUTPUT_CHARSET=UTF-8
. gettext.sh

if ! which gparted ; then
	echo "ERROR: gparted is missing"
	exit 1
fi

[ -f /usr/share/pixmaps/gparted.xpm ] & gpicon=/usr/share/pixmaps/gparted.xpm
[ -f /usr/share/pixmaps/gparted.png ] & gpicon=/usr/share/pixmaps/gparted.png
[ -z $gpicon ] && gpicon=/usr/share/pixmaps/puppy/harddisk_scsi.svg

(
	probedisk -show-usbflash | grep -v '|optical|' | \
	while IFS="|" read ONEDRIVE ONECATEG ONEDESCR ZZZ
	do
		ONEDRIVE=${ONEDRIVE##*/} #basename ONEDRIVE
		case $ONECATEG in
			drive)    ONECATEG="$(gettext 'internal drive')"  ;;
			usbdrv)   ONECATEG="$(gettext 'USB drive')"       ;;
			usbflash) ONECATEG="$(gettext 'USB Flash drive')" ;;
		esac
		echo "<radiobutton><label>${ONEDRIVE} ${ONECATEG} -- ${ONEDESCR}</label><variable>RADIO_${ONEDRIVE}</variable></radiobutton>"
	done
	echo "<radiobutton><label>$(gettext 'ALL DRIVES. Scan all drives at startup')</label><variable>RADIO_all</variable></radiobutton>"
) > /tmp/gparted_shell_probe
 
DRVTAGS="`cat /tmp/gparted_shell_probe`"

gparted_help() {
	/usr/lib/gtkdialog/box_ok "Gparted" info \
		"`gettext "At startup, Gparted scans for all drives in the computer, and on some computers this can take a very long time.. here you can choose the drive you want to work on."`" "" \
		"`gettext "<b>WARNING</b>: A PC may have a single VFAT or NTFS partition, with Windows installed, and you might want to decrease the size of that partition so as to be able to create a Linux partition. For Windows XP this is usually ok. However, Vista gets very upset if the partition is resized and may not work properly afterward -- there may be a fix for this, but we don't know what it is."`"
}
export -f gparted_help

WELCOME_MSG=$(/usr/lib/gtkdialog/xml_info fixed $gpicon 30 "$(gettext 'Gparted is an application to examine and modify partitions in a disk drive.')")
export MAIN_DIALOG='<window title="Gparted" image-name="'${gpicon}'">
<vbox>
	'${WELCOME_MSG}'
	<frame '$(gettext 'Select drive to work on')'>
		'${DRVTAGS}'
	</frame>
	<hbox>
		<button space-expand="false" space-fill="false">
			'"`/usr/lib/gtkdialog/xml_button-icon help`"'
			<label>"'$(gettext 'Help')'"</label>
			<action>gparted_help</action>
		</button>
		<button ok></button>
		<button cancel></button>
	</hbox>
</vbox>
</window>
'
rm -f /tmp/gparted_shell_probe
. /usr/lib/gtkdialog/xml_info gtk
RETVALS="`gtkdialog --center --program=MAIN_DIALOG`"
eval "$RETVALS"
[ "$EXIT" != "OK" ] && exit

THEDRIVE="`echo "$RETVALS" | grep '"true"' | cut -f 1 -d '=' | sed -e 's/RADIO_//'`"

if [ "$THEDRIVE" = "all" ] ; then
	exec gparted
fi

gparted /dev/${THEDRIVE}

sync
echo change > /sys/block/${THEDRIVE}/uevent

### END ###